//
//  CYPassWordViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/30.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYPassWordViewController.h"
#import "CYRestPasswordModel.h"
#import "CYDataSourceDelegate.h"
#import "CYPassWordCell.h"
@interface CYPassWordViewController ()<CYListTableViewDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) CYListTableView *listView;
@property (nonatomic, strong) CYDelegate *delegate;
@property (nonatomic, strong) CYDataSource *datasource;
@property (nonatomic, strong) CYButton *enterBtn;
@property (nonatomic, strong) CYRestPasswordModel *model;
@end

@implementation CYPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.hidden = self.enterBtn.hidden = NO;
    self.listView.hidden = YES;
    self.model.token = [CYCache readDataWithKey:@"token"];
    // Do any additional setup after loading the view.
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]init];
        _tableview.scrollEnabled = NO;
        [self.view addSubview:_tableview];
        [self setupTableview];
        
        [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(STATUS_HEIGHT + NAVIBAR_HEIGHT , 0, 0, 0));
        }];
    }
    
    return _tableview;
}

- (CYListTableView *)listView{
    if (!_listView) {
        _listView = [[CYListTableView alloc]initWithArray:SECURITYQUESTION];
        _listView.listTableDelegate = self;
        [self.view addSubview:_listView];
        
        [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT + AUTO(60) * 2);
            make.left.equalTo(_tableview).with.offset(20);
            make.right.equalTo(_tableview).with.offset(-20);
            make.height.mas_equalTo(AL(44) * 5);
        }];
    }
    return _listView;
}

- (CYButton *)enterBtn{
    if (!_enterBtn) {
        _enterBtn = [[CYButton alloc]initWithTitle:@"完成" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:AL(5)];
        [_enterBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [_enterBtn addTarget:self action:@selector(actionForEnterBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_enterBtn];
        
        [_enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view).with.offset(- AL(20));
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(40), AL(44)));
        }];
    }
    
    return _enterBtn;
}

- (CYRestPasswordModel *)model{
    if (!_model) {
        _model = [[CYRestPasswordModel alloc] init];
    }
    return _model;
}

- (void)setupTableview{
    WS(self)
    _delegate = [[CYDelegate alloc]initWithCellHeight:AUTO(60) views:nil block:nil];
    
    _datasource = [[CYDataSource alloc]initWithData:@[@"请输入账号",@"选择密保问题",@"答:",@"请输入旧的密码",@"设置新的密码",] identifier:@"CYPassWordCell" block:^(id cell, id data) {
        [weakSelf bindCell:cell dict:data];
    }];
    
    _tableview.delegate = _delegate;
    _tableview.dataSource = _datasource;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)bindCell:(CYPassWordCell *)cell dict:(id)dict{
    WS(self)
    cell.block = ^(NSString *placheolder, NSString *text) {
        if ([placheolder isEqualToString:@"请输入账号"] && text)
            _model.username = text;
        else if ([placheolder isEqualToString:@"选择密保问题"] && text)
            _model.question = text;
        else if ([placheolder isEqualToString:@"答:"] && text)
            _model.answer = text;
        else if ([placheolder isEqualToString:@"请输入旧的密码"] && text)
            _model.oldPassword = text;
        else if ([placheolder isEqualToString:@"设置新的密码"] && text)
            _model.nowPassword = text;
        else if ([placheolder isEqualToString:@"选择密保问题"] && !text)
            [weakSelf  showQuestionsList];
    };
    
    cell.placeholder = dict;
    if ([dict isEqualToString:@"请输入账号"])
        cell.text = [CYCache readDataWithKey:@"username"];
}

//弹出下啦列表
- (void)showQuestionsList{
    [UIView animateWithDuration:0.3 animations:^{
        _listView.hidden = NO;
    }];
}

- (NSDictionary *)para{
    
    if (_type == LoginPasswordType)
        return @{@"username":_model.username,@"question":_model.question,@"answer":_model.answer,@"password":_model.nowPassword};
    else
        return @{@"username":_model.username,@"question":_model.question,@"answer":_model.answer,@"coin_password":_model.oldPassword,@"new_password":_model.nowPassword,@"token":_model.token};
}
- (void)actionForEnterBtn{
    NSString *api = (_type == LoginPasswordType)?API_restLoginPassword:API_restMoneyPassword;
    
    self.task = [CYHttps POST:api parameters:[self para] showHUD:YES progress:nil success:^(id data) {
        if (([data isKindOfClass:[NSString class]] && _type == LoginPasswordType) || (_type == MoneyPasswordType))
            [CYSuccessTipsView showTitle:@"修改密码成功" buttonTitle:@"确定" block:^{
                if (_type == LoginPasswordType) {
                    _model.token = data;
                    [CYCache saveData:data forKey:@"token" synchronize:YES];
                }
                else if (_type == MoneyPasswordType){
                    [self popPreviousViewController];
                }
                
            }];
    } failure:nil];
    
}

#pragma mark - CYListTableViewDelegate - method
- (void)dataForSelectCell:(id)data indexPath:(NSIndexPath *)indexPath{
    _listView.hidden = YES;
    CYPassWordCell *cell = [_tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    cell.text = data;
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = (_type == LoginPasswordType)?@"修改登录密码":@"修改体现密码";
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //    [self setupNavigationBar];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
